Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Multithreading → Joining Thread

Multithreading

Joining Thread

Joining Threads in Java: Waiting for Completion

The join() method in Java's Thread class allows one thread (the calling thread) to wait for another thread (the target thread) to finish execution. This synchronization mechanism is often used to ensure that specific tasks within a thread are completed before the calling thread proceeds further.

About join():

When the join() method is invoked on a thread object, the calling thread enters a waiting state. It remains in this waiting state until the target thread terminates its execution. There are three overloaded versions of join(), providing flexibility in how long the calling thread waits: join(): Waits indefinitely for the target thread to finish. join(long millis): Waits for a specified number of milliseconds (millis) for the target thread to terminate. If the target thread doesn't finish within the specified time, the calling thread resumes execution. join(long millis, int nanos): Waits for a specified number of milliseconds (millis) and nanoseconds (nanos) for the target thread to finish. Similar to the previous overload, if the target thread doesn't terminate within the time frame, the calling thread continues execution. Example (Waiting Indefinitely):
Example of Thread joining - join() in java public class Main extends Thread { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Thread " + Thread.currentThread().getName() + " - Iteration: " + i); } } public static void main(String[] args) throws InterruptedException { Main thread = new Main(); thread.start(); // Main thread waits for the started thread to finish thread.join(); // Waits indefinitely System.out.println("Main thread execution continues..."); } }

Output

Thread Thread-0 - Iteration: 0 Thread Thread-0 - Iteration: 1 Thread Thread-0 - Iteration: 2 Thread Thread-0 - Iteration: 3 Thread Thread-0 - Iteration: 4 Main thread execution continues...

Example (Waiting with Timeout):
Example of Thread joining - join(time) in java public class Main extends Thread { @Override public void run() { for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); // Simulate a 1-second task } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { Main thread = new Main(); thread.start(); // Main thread waits for the started thread to finish, with a 2-second timeout thread.join(2000); if (thread.isAlive()) { System.out.println("Target thread is still alive (might not have finished within timeout)"); } else { System.out.println("Target thread has finished"); } System.out.println("Main thread execution continues..."); } }

Output

Target thread is still alive (might not have finished within timeout) Main thread execution continues...

Key notes: ⮚ Using join() can introduce potential deadlocks if not used carefully. A deadlock occurs when two or more threads are waiting for each other to release resources, creating a stalemate situation. ⮚ The join() method throws an InterruptedException if the calling thread is interrupted while waiting. This allows for proper handling of thread interruptions in your program. ⮚ join() doesn't guarantee that changes made by the target thread will be immediately visible to the calling thread. You might need additional synchronization mechanisms like locks to ensure proper data consistency.

Tutorials